<?php
namespace map;
//第三方地图扩展
class Map {
    /**
     * 使用CURL方式发送post请求
     * @param  $url     [请求地址]
     * @param  $data    [array格式数据]
     * @return $请求返回结果(array)
     */
    public function postDataCurl($url,$data){
        $timeout = 5000;
        $postdata = '';
        foreach ($data as $key=>$value){
            $postdata.= ($key.'='.$value.'&');
        }
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt ($ch, CURLOPT_HEADER, false );
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    
        $result = curl_exec($ch);
        if (false === $result) {
            $result =  curl_errno($ch);
        }
        curl_close($ch);
        return $this->json_to_array($result) ;
    } 
    
    /**
     * @description GET 请求
     * @return mixed
     */
   public  function get($url)
    {
        $head = [
            'User-Agent: Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
            'X-FORWARDED-FOR:154.125.25.15',
            'CLIENT-IP:154.125.25.15'
        ];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        if (false === $result) {
            $result =  curl_errno($ch);
        }
        curl_close($ch);
        return $this->json_to_array($result) ;
    }
    /**
     * 将json字符串转化成php数组
     * @param  $json_str
     * @return $json_arr
     */
    public function json_to_array($json_str){
        if(is_null(json_decode($json_str))){
            $json_str = $json_str;
        }else{
            $json_str = json_decode($json_str);
        }
        $json_arr=array();
    
        foreach($json_str as $k=>$w){
            if(is_object($w)){
                $json_arr[$k]= $this->json_to_array($w); //判断类型是不是object
            }else if(is_array($w)){
                $json_arr[$k]= $this->json_to_array($w);
            }else{
                $json_arr[$k]= $w;
            }
        }
        return $json_arr;
    }
    /**
     * 地理编码
     */
    public function getLocation($address)
    {
        $ak="WBRvogRKnKH4zSsnGiSAZgY2cVvQnAqm";
        $url = 'http://api.map.baidu.com/geocoder/v2/?address='.$address.'&output=json&ak='.$ak;
       
      
       $result=$this->get($url);
       return $result;
       unset($result);
         
    }
    /**
     * 逆地址编码 
     * @param string location 经纬度
     * @return array result 地理位置
     */
    public function getAddress($lat,$lng){
         $ak="WBRvogRKnKH4zSsnGiSAZgY2cVvQnAqm";
         $url="http://api.map.baidu.com/geocoder/v2/?location=$lat,$lng&output=json&pois=1&ak=$ak";
         $result=$this->get($url);
         return $result;
         unset($result);
    }
    //这里使用百度Web服务默认为步行的距离
    /**
     * 驾车
     */
    public function getDriving($orgins,$destinations){
        $ak="WBRvogRKnKH4zSsnGiSAZgY2cVvQnAqm";
        $url="http://api.map.baidu.com/routematrix/v2/driving?output=json&origins="
            .$orgins['lat'].",".$orgins['lng']
            ."&destinations="
            .$destinations['lat'].','.$destinations['lng']
            ."&ak="
             .$ak
            ."&tactics=13"
            ;
            
          $res=$this->get($url);
          return $res;
        
    }
    /**
     * 骑行
     */
    public function getRiding($orgins,$destinations){
        $ak="WBRvogRKnKH4zSsnGiSAZgY2cVvQnAqm";
        $url="http://api.map.baidu.com/routematrix/v2/riding?output=json&origins="
            .$orgins['lat'].",".$orgins['lng']
            ."&destinations="
                .$destinations['lat'].','.$destinations['lng']
                ."&ak="
                    .$ak
                    ."&tactics=13"
                        ;
        
                        $res=$this->get($url);
                        return $res;
    }
    /**
     * 步行
     */
    public function getWalking($orgins,$destinations){
        $ak="WBRvogRKnKH4zSsnGiSAZgY2cVvQnAqm";
        $url="http://api.map.baidu.com/routematrix/v2/walking?output=json&origins="
            .$orgins['lat'].",".$orgins['lng']
            ."&destinations="
                .$destinations['lat'].','.$destinations['lng']
                ."&ak="
                    .$ak
                    ."&tactics=13"
                        ;
        
                        $res=$this->get($url);
                        return $res;
    }
    /***
     * 坐标转换将微信传递的坐标进行转换为百度坐标
     */
    public function RetoLoadtion($lng,$lat){
          $ak="WBRvogRKnKH4zSsnGiSAZgY2cVvQnAqm";
          $url="http://api.map.baidu.com/geoconv/v1/?coords=$lng,$lat&from=1&to=5&ak=$ak";
          $res=$this->get($url);
          return $res;
          unset($res);
    }
}